<?php
	function encode($str){

		$words = explode(" ", $str);
		$result = '';
		foreach ($words as $word) {

			$opt = array();
			$opt = explode("?", $word);
			
			if(mb_strtoupper($opt[2]) == 'B'){
				$tmp_result = base64_decode($opt[3]);
			} elseif(mb_strtoupper($opt[2]) == 'Q') {
				$tmp_result = quoted_printable_decode($opt[3]);
			}
			if(mb_strtoupper($opt[1]) != 'UTF-8') {
				$tmp_result = iconv($opt[1], 'UTF-8', $tmp_result);
			}
			if(!$result) { 
				$result .= ' ';
			}
			$result .= $tmp_result;
		}

		return $result;
		
	}

	$mbox = imap_open("{imap.yandex.ru:993/ssl}INBOX", "info@poolbuild.ru", "123456") or die("can't connect: " . imap_last_error());
	$MC = imap_check($mbox);
	$mails = array();

	// Fetch an overview for all messages in INBOX
	$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
	foreach ($result as $overview) {
		print("<pre>");
		print_r($overview);
		print("</pre>");
	}

	imap_close($mbox);
?>